#include "../common.h"

const int wakeupTime = 1 * 1000 * 1000 * 10;
namespace _DeepSleepService {
void checkAndDeepSleep(void *arg = nullptr) {
	Serial.println("deepSleep appContext close");
	AppContext::close();

	uint64_t mask;
	mask = 1 << GPIO_NUM_4;
	mask |= 1 << GPIO_NUM_6;
	mask |= 1 << GPIO_NUM_7;
	esp_sleep_enable_ext1_wakeup(mask, ESP_EXT1_WAKEUP_ANY_HIGH);
	esp_sleep_enable_timer_wakeup(wakeupTime);

	//esp_sleep_enable_ext0_wakeup(GPIO_NUM_4, HIGH);
	//启动睡眠前要执行 唤醒方式
	esp_deep_sleep_start();
}

void event_serial_message(TaskCtx<String> *ctx) {
	String cmd = ctx->ctx;
	if (cmd == "stop_system" || cmd == "deep_sleep") {
		checkAndDeepSleep();
	}
}
}

void DeepSleepService::prepare0() {
	Serial.print("wake up cause : ");
	Serial.println(esp_sleep_get_wakeup_cause());

	EventService::registerHandle(EVENT_SERIAL_MESSAGE, &_DeepSleepService::event_serial_message);
}

